package com.lz.kunkun.messageservice.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.lz.kunkun.messageservice.GlobalConstant;
import com.lz.kunkun.messageservice.ServicesManager;
import com.lz.kunkun.messageservice.model.SysUser;
import com.lz.kunkun.messageservice.service.ISysUserService;

/**
 * Hello world!
 *
 */
@RestController
@RequestMapping("sys/")
public class SysUserController {
	//获取用户服务
	public ISysUserService getSysUserService() {
		return ServicesManager.instance.getISysUserService();
	}
	//获取用户信息，密码过滤掉
	@GetMapping("getUser")
	public @ResponseBody SysUser getUser(HttpServletRequest req) {
		return (SysUser) req.getSession().getAttribute("sysUser");
	}
	//用户退出
	@GetMapping("loginOut")
	public @ResponseBody void loginOut(HttpServletRequest req) {	
		req.getSession().removeAttribute("sysUser");
	}
	//用户登录
	@GetMapping("loginIn")
	public @ResponseBody Map<String,Object> loginIn(HttpServletRequest req, HttpServletResponse res,String userName,String password) {
		Map<String,Object> result=new HashMap<String,Object>();
		if(null==userName||"".equals(userName)||null==password||"".equals(password)) {
		   result.put("status",0);
		   result.put("mes", "账户或密码不能为空！");
		   return result;
		}
		SysUser user=getSysUserService().login(userName,password);
		if(user!=null) {
			HttpSession session=req.getSession();
			session.removeAttribute("sysUser");
			session.setAttribute("sysUser", user);
			result.put("status",1);
			result.put("mes", "登录成功！");
		}else {
			result.put("status",0);
			result.put("mes", "用户名或密码错误，注册或找回请联系管理员QQ："+GlobalConstant.MANAGER_QQ);
		}
		return result;
	}
	//用戶信息修改
	@GetMapping("updateUser")
	public @ResponseBody Map<String,Object> updateUserByUserId(HttpServletRequest req,SysUser user,String lostPassword) {
		Map<String,Object> result=new HashMap<String,Object>();
		HttpSession session = req.getSession();
		SysUser userSession=(SysUser) session.getAttribute("sysUser");
		if(null==lostPassword||!lostPassword.equals(userSession.getPassword())){
			result.put("status", 0);
			result.put("mes", "原密码错误，修改失败！,请联系管理员QQ:"+GlobalConstant.MANAGER_QQ);
			return result;
		}
		if(user.getName()==null||user.getName().equals("")) {
			result.put("status", 0);
			result.put("mes", "姓名不能为空！");
			return result;
		}
		if(user.getPassword()==null||user.getPassword().equals("")) {
			result.put("status", 0);
			result.put("mes", "密码不能为空！");
			return result;
		}
		if(user.getLoginName()==null||user.getLoginName().equals("")) {
			result.put("status", 0);
			result.put("mes", "登录名不能为空！");
			return result;
		}
		if(user.getPhoneNumber()==null||user.getPhoneNumber().equals("")) {
			result.put("status", 0);
			result.put("mes", "联系方式不能为空！");
			return result;
		}
		user.setUserId(userSession.getUserId());
		user.setPassword(userSession.getPassword());
		if(getSysUserService().updateUserByUserId(user)==1) {
			session.removeAttribute("sysUser");
			session.setAttribute("sysUser", user);
			result.put("status", 1);
			result.put("mes", "修改成功！");
		}else {
			result.put("status", 0);
			result.put("mes", "系统错误，修改失败！");
		}
		return result;
	}
}
